//
//  DataExtensions.swift
//  SwiftDiary-05
//
//  Created by 伏文东 on 2018/1/13.
//  Copyright © 2018年 伏文东. All rights reserved.
//

import UIKit
import CoreData

var info : UserInfoMo!

class DataExtensions {
    static func fetcAllData()  {
        do {
            info = try kContext.fetch(UserInfoMo.fetchRequest())[0]
        } catch  {
            print(error)
        }
    }
    
    static func upDataInfo(image: UIImage) {
        let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "UserInfo")
        let pro = NSPredicate(format: "sign = %d", 1)
        fetchRequest.predicate = pro
        let results =  try! kContext.fetch(fetchRequest)
        if results.count > 0 {
            for obj in (results.enumerated()) {
                let orginData = obj.element as! NSManagedObject
                orginData.setValue(UIImageJPEGRepresentation(image, 0.5), forKey: "headPortrait")
                orginData.setValue(1, forKey: "sign")
            }
        }
        do {
            try kContext.save()
        } catch  {
            fatalError("无法保存")
        }
    }
}

